package DocClass;

public class Caller {
	public static void main(String[] args)
	{
		QueryFetcher Ir = new QueryFetcher(ProcessConst.YAHOOID);
		String answer =null;
		String query;
		query=args[0];
		int round=1;
		double reqdPrecision = args.length > 1 ? Double.parseDouble(args[1]) : 0.9;
		if ( 0 > reqdPrecision || 1 < reqdPrecision ) {
			System.err.println("Precision must be between 0 and 1 (inclusive)");
			System.exit(1);
		}

		int current_round = 1;
		System.out.println("Beginning round 1: query is [" + query + "]");
		while(true){
			try {
				answer = Ir.runQuery(query);
			} catch (Exception e) {
				System.err.println(e.getMessage());
				break;
			}
			QueryProcessor m = new QueryProcessor(query);
			query = m.runProcess(answer,round);
			round++;
			if ( 0 == m.precision)
			{
				System.out.println("No relevant documents: unable to improve query.");
				break;
			}
			else if(m.precision >= reqdPrecision)
			{
				System.out.println("Current precision is " + m.precision
						+ ": desired precision reached.");
			//	System.out.println("Next query would have been " + query);
				break;
			}
			if (null != query) {
				current_round++;
				System.out.println("Current precision is " + m.precision);
				System.out.println("\nBeginning round "+ current_round +
						": new query is [" +query + "]");
			}
			else {
				throw new RuntimeException("Empty query returned: likely an internal error");
			}

		}
	}
}
